﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Submarines
{
    public partial class Board : UserControl
    {
        Random r = new Random();
        public Color _OldBackColor;
        public Color _ExistColor = Color.Gold;
        public Color _ShadowColor = Color.Black;
        public Color _HitColor = Color.Red;
        public string _Exists = "Exists";
        public string _Normal = "Normal";
        public string _Hit = "Hit";
        Button _tempButton;
       // Button[,] _Matrix = new Button[10, 10];
        int[] _Count = new int[4];
        
        public Board()
        {
            InitializeComponent();
        }

        private void Board_Load(object sender, EventArgs e)
        {
            for (int x = 0; x < 10; x++)
                for (int y = 0; y < 10; y++)
                {
                    _tempButton = new Button();
                   // _Matrix[x, y] = new Button();
                   // _Matrix[x, y].Location = new Point(10 + x * 30, 10 + y * 30);
                    _tempButton.Dock = DockStyle.Fill;
                    _tempButton.FlatStyle = FlatStyle.Flat;
                    _tempButton.Margin = new Padding(0);
                    _tempButton.Name = x.ToString() + "," + y.ToString();
                    _tempButton.Size = new Size(30, 30);
                    _tempButton.AllowDrop = true;
                    _tempButton.MouseClick += new MouseEventHandler(Board_MouseClick);
                    _tempButton.DragEnter += new DragEventHandler(Board_DragEnter);
                    _tempButton.DragLeave += new EventHandler(Board_DragLeave);
                    _tempButton.DragDrop += new DragEventHandler(Board_DragDrop);
                    //Controls.Add(_Matrix[x, y]);
                    tblPanel.Controls.Add(_tempButton, x, y);
                }
            InitializeSubmarines();
        }

        void Board_MouseClick(object sender, MouseEventArgs e)
        {
            OnMouseClick(sender, e);
        }

        void Board_DragDrop(object sender, DragEventArgs e)
        {
            OnDragDrop(sender, e);
        }

        void Board_DragLeave(object sender, EventArgs e)
        {
            OnDragLeave(sender, e);
        }

        void Board_DragEnter(object sender, DragEventArgs e)
        {
            OnDragEnter(sender, e);
        }

        protected virtual void OnMouseClick(object sender, MouseEventArgs e)
        {
            string[] cord;
            Button btn = (Button)sender as Button;
            cord = btn.Name.Split(',');

            CheckIfHit(int.Parse(cord[0]), int.Parse(cord[1]));
        }


        bool CheckIfHit(int row, int col)
        {
            if(true)
            {
                return false;
            }
            // function PUSH
            else{
            return TryHit(row, col);}
            
        }

        protected virtual void OnDragDrop(object sender, DragEventArgs e)
        {
        }

        protected virtual void OnDragLeave(object sender, EventArgs e)
        {
        }

        protected virtual void OnDragEnter(object sender, DragEventArgs e)
        {
        }

        public int GetSize(string s)
        {
            return int.Parse(s.Substring(1));
        }

        public bool GetShape(string s)
        {
            return (s.Substring(0, 1) == "V");
        }

        public int GetX(string s)
        {
            return (int.Parse(s.Substring(0, s.IndexOf(',') )));
        }

        public int GetY(string s)
        {
            return (int.Parse(s.Substring(s.IndexOf(',') + 1, s.Length - s.IndexOf(',') - 1)));
        }

        public bool SubmarineValid(int x, int y, int size, bool Vertical)
        {
            if (_Count[size - 1] == 0) return false;

            if (Vertical)
            {
                // is submarine out of boundary
                if (10 - y < size) return false;
                // check if visible
                for (int b = y; b <= y + size - 1; b++)
                    if (((Button)tblPanel.GetControlFromPosition(x, b)).Visible == false) return false;
                // check 3 columns
                for (int a = x - 1; a <= x + 1; a++)
                    for (int b = y - 1; b <= y + size; b++)
                        if (a >= 0 && a <= 9 && b >= 0 && b <= 9)
                            if ((string)((Button)tblPanel.GetControlFromPosition(a,b)).Tag == _Exists) return false;
            }
            else
            {
                // is submarine out of boundary
                if (10 - x < size) return false;
                // check if visible
                for (int a = x; a <= x + size - 1; a++)
                    if (((Button)tblPanel.GetControlFromPosition(a,y)).Visible == false) return false;
                // check 3 rows
                for (int a = x - 1; a <= x + size; a++)
                    for (int b = y - 1; b <= y + 1; b++)
                        if (a >= 0 && a <= 9 && b >= 0 && b <= 9)
                            if ((string)((Button)tblPanel.GetControlFromPosition(a,b)).Tag == _Exists) return false;
            }
            return true;
        }
        
        public void DrawSubmarine(int x, int y, int size, bool vertical, Color color)
        {
           
            if (vertical)
            {
                for (int a = y; a < y + size; a++)
                   ((Button)tblPanel.GetControlFromPosition(x, a)).BackColor = color;
              
                    //_Matrix[x, a].BackColor = color;
            }
            else
            {
                for (int a = x; a < x + size; a++)
                    ((Button)tblPanel.GetControlFromPosition(a, y)).BackColor = color;
            }
        }

        public void UpdateSubmarine(int x, int y, int size, bool vertical, string state)
        {
            if (vertical)
            {
                for (int a = y; a < y + size; a++)
                    ((Button)tblPanel.GetControlFromPosition(x, a)).Tag = state;
            }
            else
            {
                for (int a = x; a < x + size; a++)
                    ((Button)tblPanel.GetControlFromPosition(a,y)).Tag = state;
            }
        }

        public void InstallSubmarine(int size)
        {
            _Count[size - 1]--;
        }

        public void InitializeSubmarines()
        {
            _Count[0] = 4;
            _Count[1] = 3;
            _Count[2] = 2;
            _Count[3] = 1;

            for (int x = 0; x < 10; x++)
                for (int y = 0; y < 10; y++)
                {
                    ((Button)tblPanel.GetControlFromPosition(x, y)).BackColor = Color.LightGray;
                    ((Button)tblPanel.GetControlFromPosition(x, y)).Tag = _Normal;
                }
        }

        public bool TryHit(int x, int y)
        {
            if ((string)((Button)tblPanel.GetControlFromPosition(x, y)).Tag == "Hit") return false;
            if (((Button)tblPanel.GetControlFromPosition(x, y)).Visible == false) return false;
            if ((string)((Button)tblPanel.GetControlFromPosition(x, y)).Tag == _Exists)
            {
                ((Button)tblPanel.GetControlFromPosition(x, y)).Tag = _Hit;
                ((Button)tblPanel.GetControlFromPosition(x, y)).BackColor = _HitColor;
            }
            else
            {
                ((Button)tblPanel.GetControlFromPosition(x, y)).Visible = false;
                
            }
            return true;
        }

        // gai


        bool NewSubmarine(int size)
        {
            bool vertical = (r.Next(0, 2) == 1);
            int x = r.Next(0, 10);
            int y = r.Next(0, 10);

            if (SubmarineValid(x, y, size, vertical))
            {
                DrawSubmarine(x, y, size, vertical, _ExistColor);
                UpdateSubmarine(x, y, size, vertical, _Exists);
                InstallSubmarine(size);
                return true;
            }
            return false;
        }

        public void InstallSubmarines()
        {
            int g;

            InitializeSubmarines();
            for (int a = 1; a <= 4; a++)
                for (int b = a; b <= 4; b++)
                    while (!NewSubmarine(a)) g = 1;
        }

        public void Next()
        {
            int x, y;
            do
            {
                x = r.Next(0, 10);
                y = r.Next(0, 10);
            }
            while (!TryHit(x, y));

        }


    }
}
